package com.df.bpm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.df.bpm.dto.BusinessInformationDTO4Subset;
import com.df.bpm.dto.BusinessStepDTO;
import com.df.bpm.entity.BusinessInformation;
import com.df.bpm.entity.BusinessLocations;
import com.df.bpm.service.impl.BusinessInformationDTO4SubsetServiceImpl;
import com.df.bpm.service.impl.BusinessLocationsServiceImpl;
import com.df.common.utils.CommonUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.df.bpm.service.impl.BusinessInformationServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 业务大全控制器
 */
@Slf4j
@Tag(name = "业务大全控制器")
@RequestMapping("/businessInformation")
@RequiredArgsConstructor
@RestController
@SaCheckLogin
public class BusinessInformationController {
    /**
     * 业务信息服务
     */
    public final BusinessInformationServiceImpl businessInformationServiceImpl;

    /**
     * 业务信息服务
     */
    public final BusinessLocationsServiceImpl businessLocationsServiceImpl;

    /**
     * 业务信息DTO服务
     * <p>subset</p>
     */
    public final BusinessInformationDTO4SubsetServiceImpl businessInformationDTO4SubsetServiceImpl;


    /**
     * 更新（业务信息）
     * @param steps 业务信息(包含业务定位)
     */
    @Operation(summary = "更新（业务信息）", description = "更新（业务信息）")
    @PostMapping("update")
    @SaIgnore
    public ResponseEntity<?> update(@RequestBody Map<Integer,BusinessStepDTO> steps){
        int result=0;
        for(Map.Entry<Integer,BusinessStepDTO>step:steps.entrySet()){
            BusinessInformation businessInformation=step.getValue().getBusiness();
            BusinessLocations businessLocations=step.getValue().getLocation();
            if(CommonUtils.varIsBlank(businessInformation)){
                return null;
            }
            if(CommonUtils.varIsBlank(businessLocations)){
                return null;
            }
            if(businessInformation.getBusinessInformationIndex()==null){
                result+=businessInformationServiceImpl.add(businessInformation);
            }else{
                result+=businessInformationServiceImpl.update(businessInformation);
            }
            businessLocations.setBusinessInformationIndex(
                    businessInformation.getBusinessInformationIndex()
            );
            if(businessLocations.getBusinessLocationsIndex()==null){
                result+=businessLocationsServiceImpl.add(businessLocations);
            }else{
                result+=businessLocationsServiceImpl.update(businessLocations);
            }

        }
        return ResponseEntity.ok(result);
    }

    /**
     * 删除（业务信息）
     * @param businessInformationIndex 业务信息ID
     */
    @Operation(summary = "删除（业务信息）", description = "删除（业务信息）")
    @GetMapping("delete")
    @SaIgnore
    public ResponseEntity<?> delete(@Parameter Integer businessInformationIndex){
        return ResponseEntity.ok(businessInformationServiceImpl.deleteDeeply(businessInformationIndex));
    }

    /**
     * 删除（业务信息）
     * @param businessLocationsIndex 业务定位ID
     */
    @Operation(summary = "删除（业务定位）", description = "删除（业务定位）")
    @GetMapping("deleteLocation")
    @SaIgnore
    public ResponseEntity<?> deleteLocation(@Parameter Integer businessLocationsIndex){
        return ResponseEntity.ok(businessLocationsServiceImpl.deleteAndReSequence(businessLocationsIndex));
    }


    /**
     * （业务信息）调整顺序
     * <P>head2Tail</P>
     * @param headSequence  待更改顺序的序号
     * @param tailSequence  目标位置序号
     * @param superior  父级业务id
     * @param groupID   分组id
     * @return 更改结果
     */
    @Operation(summary = "（业务信息）调整顺序", description = "（业务信息）调整顺序")
    @GetMapping("updateSequence")
    @SaIgnore
    public ResponseEntity<?> updateSequence(@Parameter Integer headSequence,
                                            @Parameter Integer tailSequence,
                                            @Parameter Integer superior,
                                            @Parameter Integer groupID){
        if(headSequence.equals(tailSequence)){
            return ResponseEntity.ok(0);
        }
        BusinessLocations head=businessLocationsServiceImpl.getOne(new BusinessLocations()
                .setSuperior(superior)
                .setGroupID(groupID)
                .setSequence(headSequence));
        int Length=1;
        if(headSequence<tailSequence){
            Length=-1;
        }
        businessLocationsServiceImpl.updateSequences(Length,headSequence,tailSequence,superior,groupID);
        businessLocationsServiceImpl.update(new BusinessLocations()
                .setBusinessLocationsIndex(head.getBusinessLocationsIndex())
                .setSequence(tailSequence));
        return ResponseEntity.ok(1);
    }

    /**
     * 获取业务信息
     * @param superior 上级业务信息 -1=根
     * @param groupID 上级业务信息 -1=不分组
     */
    @Operation(summary = "获取业务子级流程信息", description = "获取业务子级流程信息")
    @GetMapping("getSteps")
    @SaIgnore
    public ResponseEntity<Map<Integer, BusinessStepDTO>> getSteps(@RequestParam Integer groupID, @RequestParam Integer superior) {
        List<BusinessLocations> locations = businessLocationsServiceImpl.getSteps(groupID, superior);
        Map<Integer, BusinessStepDTO> result=null;
        if(CommonUtils.varIsNotBlank(locations)){
            result=new HashMap<>();
            for(BusinessLocations location : locations){
                BusinessInformationDTO4Subset temp=new BusinessInformationDTO4Subset();
                temp.setBusinessInformationIndex(location.getBusinessInformationIndex());
                BusinessInformationDTO4Subset business=businessInformationDTO4SubsetServiceImpl.getOne(temp);
                result.put(location.getSequence(), new BusinessStepDTO(location,business));
            }
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 获取业务子集分组信息
     * @param superior 业务ID -1=根
     */
    @Operation(summary = "获取业务子集分组信息", description = "获取业务子集分组信息")
    @GetMapping("getGroups")
    @SaIgnore
    public ResponseEntity<List<Integer>> getGroups(@RequestParam(required = false) Integer superior) {
        if (CommonUtils.varIsBlank(superior)){
            superior = -1;
        }
        return ResponseEntity.ok(businessLocationsServiceImpl.getGroups(superior));
    }

}
